//
//  Emoticon.swift
//  表情包
//
//  Created by miaozhihui on 2017/5/5.
//  Copyright © 2017年 DeKuTree. All rights reserved.
//

import UIKit
import YYModel

/// 表情模型
class Emoticon: NSObject {

    /// 表情类型 false - 图片表情 true - emoji
    var type = false
    
    /// 表情字符转，发送给服务器(解约流量)
    var chs: String?
    
    /// 表情图片名称，用于本地图文混排
    var png: String?
    
    /// emoji 的十六进制编码
    var code: String?
    
    /// 表情模型所在的目录
    var directory: String?
    
    /// 图片表情对应的图像
    var image: UIImage? {
        if type {
            return nil
        }
        guard let directory = directory,
            let png = png,
            let path = Bundle.main.path(forResource: "Emoticon.bundle", ofType: nil),
            let bundle = Bundle(path: path)
            else {
            return nil
        }
        return UIImage(named: "\(directory)/\(png)", in: bundle, compatibleWith: nil)
    }
    
    func imageText(font: UIFont) -> NSAttributedString {
        guard let image = image else {
            return NSAttributedString(string: "")
        }
        let attachment = NSTextAttachment()
        attachment.image = image
        let height = font.lineHeight
        attachment.bounds = CGRect(x: 0, y: -4, width: height, height: height)
        return NSAttributedString(attachment: attachment)
    }
    
    override var description: String {
        return yy_modelDescription()
    }
    
}
